import { AppCTX } from "../../types/koaExtend";
import { createStoreApiMWRouter } from "../../middleware/store-api/storeOwnerChecker";
import {
  aggrActivityStatsOfStoreByActivityID,
  aggrConsignmentByActivityID,
  aggrConsignmentForSpokesmanByActivityID,
  rankOverOrderAmountForSpokesmanByActivityID,
} from "../../service/activityStatService";
const router = createStoreApiMWRouter();

// 获取活动帮卖统计
router.get(
  "/sto/:storeId/activity/:activityId/statistics/consignment",
  async (ctx: AppCTX, next) => {
    const { storeId, activityId } = ctx.params;
    const { userId } = ctx;

    const activityConsignmentAggrInfo = await aggrConsignmentByActivityID(
      activityId
    );
    const spokesmanActivityConsignmentAggrInfos = await aggrConsignmentForSpokesmanByActivityID(
      activityId
    );
    ctx.body = {
      activityConsignmentAggrInfo,
      spokesmanActivityConsignmentAggrInfos,
    };
  }
);

// 获取门店活动收入统计
router.get(
  "/sto/:storeId/activity/:activityId/statistics/income",
  async (ctx: AppCTX, next) => {
    const { storeId, activityId } = ctx.params;
    const { userId } = ctx;

    const aggrRet = await aggrActivityStatsOfStoreByActivityID(
      activityId,
      storeId
    );
    ctx.body = aggrRet;
  }
);

// 获取活动代言人排名
router.get(
  "/sto/:storeId/activity/:activityId/statistics/spokesmanRank",
  async (ctx: AppCTX, next) => {
    const { brandId, activityId } = ctx.params;
    const { topRankNumber } = ctx.query;
    const { userId } = ctx;

    const rankOverRet = await rankOverOrderAmountForSpokesmanByActivityID(
      activityId,
      topRankNumber ?? 5
    );
    ctx.body = rankOverRet;
  }
);

export default router;
